﻿using DevComponents.DotNetBar;
using RFID.BLL;
using System.Windows.Forms;

namespace RFID.PC
{
    public partial class ChildForm : Office2007Form
    {
        public MainForm MainForm { get; private set; }

        public string Title { get; private set; }

        public ChildForm()
        {
            InitializeComponent();

            MaximizeBox = false;
            MinimizeBox = false;
            FormBorderStyle = FormBorderStyle.FixedToolWindow;
        }

        public ChildForm(MainForm parent, object sender)
            : this()
        {
            InitializeComponent();
            MdiParent = parent;
            MainForm = parent;

            if (sender is ButtonItem button)
            {
                Title = button.Text;
            }
        }

        protected void GetTagIdByDoubleClick(object sender)
        {
            if (sender is TextBox txt)
            {
                if (MainForm.ComEnable)
                {
                    byte[] data = new byte[12];
                    if (EPCSDKHelper.ReadTag(MainForm.Com, 0x01, 2, 6, data, 0))
                    {
                        txt.Text = ConvertHelper.ByteArrayToHexString(data);
                    }
                }
                else
                {
                    ShowMessage("未打开串口！", false);
                }
            }
        }

        protected void ShowMessage(string message, bool success = true)
        {
            if (success)
            {
                MessageBox.Show(message, "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show(message, $"{Title}失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        protected void ShowResult<T>(OperateResult<T> result)
        {
            if (result.Status)
            {
                ShowMessage($"恭喜你，{Title}成功啦！", true);
            }
            else
            {
                ShowMessage(result.Message, false);
            }
            Close();
        }
    }
}
